//
//  RequestManageController.m
//  healthApp
//
//  Created by william mu on 12-8-29.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "RequestManageController.h"
#import "ProtocolService.h"
#import "GlobalState.h"
#import "RequestCell.h"

@interface RequestManageController ()

@end

@implementation RequestManageController
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"请求管理";
    m_all_request = nil;
}

-(void) viewWillAppear:(BOOL) animated
{
    
    [m_userBaseInfo release];
    m_userBaseInfo = [[NSMutableDictionary dictionary] retain];
    [self startRequest];
}

-(void) startRequest
{
    GlobalState* state = [GlobalState sharedInstance];
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol setAction:@selector(onGetFriendRequest:)];
    [protocol setTarget:self];
    [protocol getFriendRequest:state.uid];
}

-(void) onGetFriendRequest:(NSString*) json
{
    NSArray* ary = [(NSArray*)[json JSONValue] retain];
    
    m_all_request = [[NSMutableArray array] retain];
    
    for(NSDictionary* req in ary)
    {
        if([[req objectForKey:@"handled"] isEqual:@"1"])
            continue;
        
        [m_all_request addObject:req];
    }

ProtocolService* protocol = [ProtocolService sharedInstance];
for(NSDictionary* req in ary){
    NSString* reqUid = [req objectForKey:@"reqUid"];
    [protocol setTarget:self];
    [protocol setAction:@selector(onGetUserBaseInfo:)];
    [protocol requestUserBaseInfo:reqUid];
}
}

-(void) onGetUserBaseInfo:(NSString*) json
{
    NSDictionary* baseInfo = (NSDictionary*)[json JSONValue];
    
    NSString* friendUid = [baseInfo objectForKey:@"uid"];
    
    [m_userBaseInfo setObject:baseInfo forKey:friendUid];
    
    if([m_userBaseInfo count] == [m_all_request count])
        [self.tableView reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    if(m_all_request)
        [m_all_request release], m_all_request = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSString*) getReqIDForIndexPath:(NSIndexPath*) indexPath
{
    NSUInteger row = [indexPath row];
    
    NSDictionary* requestInfo = [m_all_request objectAtIndex:row];
    
    NSString* reqUid = [requestInfo objectForKey:@"reqUid"];
    
    return reqUid;
}

-(NSString*) getFriendNameForIndexPath:(NSIndexPath*) indexPath
{
    NSString* reqUid = [self getReqIDForIndexPath:indexPath];
    NSDictionary* baseInfo = [m_userBaseInfo objectForKey:reqUid];
    NSString* lastName = [baseInfo objectForKey:@"lastName"];
    NSString* firstName = [baseInfo objectForKey:@"firstName"];
    
    NSString* name = [NSString stringWithFormat:@"%@%@", lastName, firstName];
    
    return name;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(!m_all_request)
        return 0;
    
    return m_all_request.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RequestCell* cell = (RequestCell*)[tableView dequeueReusableCellWithIdentifier: @"RequestCell"];
    if(!cell)
    {
        NSArray* ary = [[NSBundle mainBundle] loadNibNamed:@"RequestCell" owner:self options:nil];
        
        cell = (RequestCell*)[ary objectAtIndex:0];
    }
    
    cell.name.text = [self getFriendNameForIndexPath:indexPath];
    
    cell.segHandle.selectedSegmentIndex = 0;
    
    cell.segHandle.tag = (NSUInteger)[self getReqIDForIndexPath:indexPath];
    
    [cell.segHandle addTarget:self action:@selector(onSegSelect:) forControlEvents:UIControlEventValueChanged];

    return cell;
}

-(void) removeCell:(NSString*) reqUid
{
    NSDictionary* req = nil;
    
    for(NSDictionary* dic in m_all_request)
    {
        if([[dic objectForKey:@"reqUid"] isEqual:reqUid])
        {
            req = dic;
            break;
        }
    }
    
    [m_all_request removeObject:req];
    [self.tableView reloadData];
}

-(void) onSegSelect:(UISegmentedControl*) seg
{
    if (seg.selectedSegmentIndex == 0) {
        NSLog(@"Ignore tapped!");
        return;
    }
    
    NSString* reqUid = (NSString*)seg.tag;
    
    NSString* accept = seg.selectedSegmentIndex == 1 ? @"true" : @"false";
    
    ProtocolService* protocol = [ProtocolService sharedInstance];
    [protocol handleFriendReq:reqUid accept:accept];
    
    [self removeCell:reqUid];
    
}


@end
